package com.example.service.impl;

import com.example.mapper.OrderMapper;
import com.example.mapper.UserMapper;
import com.example.mapper.WriteOffMapper;
import com.example.pojo.entity.Order;
import com.example.pojo.entity.User;
import com.example.pojo.entity.WriteOff;
import com.example.service.WriteOffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class WriteOffServiceImpl implements WriteOffService {

    @Resource
    private WriteOffMapper writeOffMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public PageInfo<Order> selectByPage(Integer pageNum, Integer pageSize, String name, String phone) {
        Integer type = 0;
        Integer userId = userMapper.selectByPhone(phone).getId();

        List<Order> list = orderMapper.selectByUserId(userId,type);

        Iterator<Order> iterator = list.iterator();
        while (iterator.hasNext()) {
            Order order = iterator.next();
            if (writeOffMapper.selectByOrderNum(order.getOrderNum()).getState()) {
                iterator.remove(); // 安全地移除元素
            }
        }
        PageHelper.startPage(pageNum, pageSize);

/*
        for (Order order : list) {
           if (writeOffMapper.selectByOrderNum(order.getOrderNum()).getState() == true){
               list.remove(order);
           }
        }*/
        return new PageInfo<>(list);
    }

    @Override
    public void insert(WriteOff writeOff) {

        writeOffMapper.insert(writeOff);

    }

    @Override
    public void updateWriteOff(WriteOff writeOff) {
        writeOff.setState(true);
        writeOffMapper.updateWriteOff(writeOff);
    }
}
